"""
作者：李明珠
功能：芯片平均间距计算
版本:1.1
日期：2021-1-18
"""


# 读取‘基板代码表’查询基板信息。
# data = pd.DataFrame (pd.read_excel('LM-F302001-1 基板代码表20200907.xlsx'))

def get_interval(diameter, String, Parallel, chip_length, chip_wide, Dam_Dispensing):
    # data = pd.read_sql(f"SELECT * FROM RD_Substrate_Code WHERE Substrate_Code='{Substrate_code}' AND DataStatus=1",
    #                    engine)
    # Substrate_code1 = eval(Substrate_code)
    # result = data.loc[data['基板代码'] == Substrate_code]

    # 输入镜面直径、串并数、芯片长宽、围坝层数（1为常规围坝，2为双层围坝）信息
    # diameter = result.loc[0, 'LES_mm']

    # 将镜面直径、串并数、芯片长宽信息转换成数值
    D = int(diameter)
    S = int(String)
    P = int(Parallel)
    chip_L = float(chip_length)
    chip_W = float(chip_wide)
    Dam = int(Dam_Dispensing)

    # 计算芯片面积、芯片总数、镜面面积、芯片总面积占镜面面积之比、镜面偏移、有效面积、芯片间距
    chip_area = chip_L * 0.0254 * chip_W * 0.0254
    chip_sum = S * P
    D_area = pow(D / 2, 2) * 3.14
    chip_than = chip_area * chip_sum / D_area

    if Dam == 1 and chip_than > 0.5:
        D_Offset = 0.2
    elif Dam == 1 and 0.3 < chip_than <= 0.5:
        D_Offset = 0.3
    elif Dam == 1 and 0.23 < chip_than <= 0.3:
        D_Offset = 0.4
    elif Dam == 1 and 0.17 < chip_than <= 0.23:
        D_Offset = 0.5
    elif Dam == 1 and 0.15 < chip_than <= 0.17:
        D_Offset = 0.55
    elif Dam == 1 and 0.13 < chip_than <= 0.15:
        D_Offset = 0.65
    elif Dam == 1 and 0.11 < chip_than <= 0.13:
        D_Offset = 0.75
    elif Dam == 1 and 0.09 < chip_than <= 0.11:
        D_Offset = 0.85
    elif Dam == 1 and chip_than <= 0.09:
        D_Offset = 1.1
    elif Dam == 2 and chip_than > 0.5:
        D_Offset = 0.7
    elif Dam == 2 and 0.3 < chip_than <= 0.5:
        D_Offset = 0.8
    elif Dam == 2 and 0.23 < chip_than <= 0.3:
        D_Offset = 0.8
    elif Dam == 2 and 0.17 < chip_than <= 0.23:
        D_Offset = 0.8
    elif Dam == 2 and 0.15 < chip_than <= 0.17:
        D_Offset = 0.8
    elif Dam == 2 and 0.13 < chip_than <= 0.15:
        D_Offset = 0.9
    elif Dam == 2 and 0.11 < chip_than <= 0.13:
        D_Offset = 1
    elif Dam == 2 and 0.09 < chip_than <= 0.11:
        D_Offset = 1.1
    elif Dam == 2 and chip_than <= 0.09:
        D_Offset = 1.2
    active_area = pow((D - D_Offset) / 2, 2) * 3.14
    Chip_spacing = (active_area - chip_area * chip_sum) / chip_sum / 2
    r = D / 2

    # 打印输出结果
    # print('芯片面积=', "%.4f" % chip_area, )
    # print('芯片总数=', chip_sum)
    # print('镜面面积=', "%.4f" % D_area)
    # print('芯片比=', "%.4f" % chip_than)
    # print('镜面偏移=', "%.4f" % D_Offset)
    # print('有效面积=', "%.4f" % active_area)
    # print('芯片间距=', "%.4f" % Chip_spacing)
    return round(D_Offset,4), round(Chip_spacing,4)


if __name__ == '__main__':
    get_interval(15, 10, 4, 34 * 0.0254, 17 * 0.0254, 1)
